
package com.lianniu.mgr.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.dao.IProductPicDao;
import com.lianniu.mgr.product.model.ProductCateVo;
import com.lianniu.mgr.product.model.ProductModelVo;
import com.lianniu.mgr.product.model.ProductPicVo;
import com.lianniu.mgr.product.service.IProductPicService;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ProductPicServiceImpl implements IProductPicService{

	@Autowired
	IProductPicDao productPicDao;

	private static final Logger logger = Logger.getLogger(ProductPicServiceImpl.class);

	@Override
	public BaseResult insertProductPic(ProductPicVo curProductPicVo) {
		BaseResult baseResult;
	    if (StringUtil.isEmpty(curProductPicVo.getPicName())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
			    
		
	    return BaseResult.success(productPicDao.insertProductPic(curProductPicVo));
	}

	@Override
	public BaseResult selectByProductPicForPage(ProductPicVo curProductPicVo, PaginationInfo paginationInfo) {
		if (!StringUtil.isEmpty(curProductPicVo.getPicName())) {
			curProductPicVo.setPicName(curProductPicVo.getPicName().replace(" ", ""));
	    }
	    
	    
	    curProductPicVo.setPaginationInfo(paginationInfo);
	    List<ProductPicVo> list=productPicDao.selectByProductPicForPage(curProductPicVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult updateById(ProductPicVo curProductPicVo) {
		 BaseResult baseResult;
		 if (StringUtil.isEmpty(curProductPicVo.getPicName())) {
		      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
		 
		    return BaseResult.success(productPicDao.updateById(curProductPicVo));
	}

	@Override
	public BaseResult deleteById(String picId) {
		 return BaseResult.success(productPicDao.deleteById(picId));
	}

	@Override
	public BaseResult selectById(ProductPicVo curProductPicVo) {
		if (!StringUtil.isEmpty(curProductPicVo.getCateParentName())) {
			curProductPicVo.setCateParentName(curProductPicVo.getCateParentName().replace(" ", ""));
	    }
		return BaseResult.success(productPicDao.selectById(curProductPicVo.getPicId()));
	}

	@Override
	public Boolean checkPicName(String picName) {
		if (StringUtil.isEmpty(picName)) {       
		      return false;
		    }
		    
		    ProductPicVo productPic=new ProductPicVo();	   
		    productPic.setPicName(picName);
		    int count=productPicDao.count(productPic);
		    if(count>0){
		      return false;      
		    }
		    else{
		      return true;
		    }   
	}

	@Override
	public BaseResult deleteProductPic(String... picId) {
		 BaseResult baseResult;
		    if (StringUtil.isEmpty(picId.toString())) {
		      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
		    
		    return BaseResult.success(productPicDao.deleteProductPic(picId));
	}

	@Override
	public BaseResult getByPrentId(String parentId) {
		
		logger.info("getByParentId begin");
		logger.info("parentId:"+parentId);
		if(StringUtil.isEmpty(parentId)){
	      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	    }
		List<ProductCateVo> list=productPicDao.getByPrentId(parentId);
		logger.info("getByParentId end");
		return BaseResult.success(list);
	}
	@Override
	public BaseResult productPicSelectById(String picId) {
		 BaseResult baseResult;
		    if (StringUtil.isEmpty(picId.toString())) {
		      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }
		    
		    return BaseResult.success(productPicDao.productPicSelectById(picId));
	}

	@Override
	public BaseResult getByCateId(String cateId) {
		if(StringUtil.isEmpty(cateId)){
		      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		    }
		List<ProductPicVo> list=productPicDao.getByCateId(cateId);
		return BaseResult.success(list);
	}

	

}
